<template>
  <div class="login-container">
    <div class="login-box">
      <div class="welcome-section">
        <h1>智能家居管理系统</h1>
        <p>让生活更智能，让家居更舒适</p>
      </div>

      <el-card class="login-card" :body-style="{ padding: '30px' }">
        <template #header>
          <div class="card-header">
            <h2>{{ isLogin ? '用户登录' : '用户注册' }}</h2>
          </div>
        </template>
        
        <!-- 登录表单 -->
        <el-form v-if="isLogin" 
          :model="loginForm" 
          :rules="loginRules" 
          ref="loginFormRef"
          class="login-form">
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              :prefix-icon="User"
              size="large">
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              :prefix-icon="Lock"
              show-password
              size="large">
            </el-input>
          </el-form-item>
          <div class="form-options">
            <el-checkbox v-model="loginForm.remember">记住我</el-checkbox>
            <el-link type="primary" :underline="false">忘记密码？</el-link>
          </div>
          <el-form-item>
            <el-button 
              type="primary" 
              @click="handleLogin" 
              class="submit-btn"
              size="large"
              :loading="loading">
              登录
            </el-button>
          </el-form-item>
        </el-form>

        <!-- 注册表单 -->
        <el-form v-else 
          :model="registerForm" 
          :rules="registerRules" 
          ref="registerFormRef"
          class="register-form">
          <el-form-item prop="username">
            <el-input 
              v-model="registerForm.username"
              placeholder="请输入用户名"
              :prefix-icon="User"
              size="large">
            </el-input>
          </el-form-item>
          <el-form-item prop="email">
            <el-input 
              v-model="registerForm.email"
              placeholder="请输入邮箱"
              :prefix-icon="Message"
              size="large">
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input 
              v-model="registerForm.password"
              type="password"
              placeholder="请输入密码"
              :prefix-icon="Lock"
              show-password
              size="large">
            </el-input>
          </el-form-item>
          <el-form-item prop="confirmPassword">
            <el-input 
              v-model="registerForm.confirmPassword"
              type="password"
              placeholder="请确认密码"
              :prefix-icon="Lock"
              show-password
              size="large">
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button 
              type="primary" 
              @click="handleRegister" 
              class="submit-btn"
              size="large"
              :loading="loading">
              注册
            </el-button>
          </el-form-item>
        </el-form>

        <div class="form-footer">
          <p>
            {{ isLogin ? '还没有账号？' : '已有账号？' }}
            <el-link 
              type="primary" 
              :underline="false"
              @click="toggleForm">
              {{ isLogin ? '立即注册' : '立即登录' }}
            </el-link>
          </p>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, Message } from '@element-plus/icons-vue'

export default {
  name: 'Login',
  setup() {
    const router = useRouter()
    const loginFormRef = ref(null)
    const registerFormRef = ref(null)
    const loading = ref(false)
    const isLogin = ref(true)

    // 登录表单
    const loginForm = reactive({
      username: '',
      password: '',
      remember: false
    })

    // 注册表单
    const registerForm = reactive({
      username: '',
      email: '',
      password: '',
      confirmPassword: ''
    })

    // 登录验证规则
    const loginRules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
      ]
    }

    // 注册验证规则
    const registerRules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
      ],
      email: [
        { required: true, message: '请输入邮箱地址', trigger: 'blur' },
        { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
      ],
      confirmPassword: [
        { required: true, message: '请确认密码', trigger: 'blur' },
        {
          validator: (rule, value, callback) => {
            if (value !== registerForm.password) {
              callback(new Error('两次输入的密码不一致'))
            } else {
              callback()
            }
          },
          trigger: 'blur'
        }
      ]
    }

    // 处理登录
    const handleLogin = () => {
      loginFormRef.value.validate((valid) => {
        if (valid) {
          loading.value = true
          // 模拟登录请求
          setTimeout(() => {
            loading.value = false
            ElMessage.success('登录成功')
            router.push('/')
          }, 1500)
        }
      })
    }

    // 处理注册
    const handleRegister = () => {
      registerFormRef.value.validate((valid) => {
        if (valid) {
          loading.value = true
          // 模拟注册请求
          setTimeout(() => {
            loading.value = false
            ElMessage.success('注册成功')
            isLogin.value = true
          }, 1500)
        }
      })
    }

    // 切换登录/注册表单
    const toggleForm = () => {
      isLogin.value = !isLogin.value
    }

    return {
      loginForm,
      registerForm,
      loginFormRef,
      registerFormRef,
      loginRules,
      registerRules,
      loading,
      isLogin,
      handleLogin,
      handleRegister,
      toggleForm,
      // 图标
      User,
      Lock,
      Message
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-container {
  width: 100%;
  max-width: 1000px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 30px;
}

.welcome-section {
  text-align: center;
  color: white;
  margin-bottom: 20px;
}

.welcome-section h1 {
  font-size: 2.5em;
  margin: 0;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
}

.welcome-section p {
  font-size: 1.2em;
  margin: 10px 0 0;
  opacity: 0.9;
}

.login-card {
  width: 100%;
  max-width: 400px;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
}

.card-header {
  text-align: center;
  padding: 20px 0;
}

.card-header h2 {
  margin: 0;
  font-size: 24px;
  color: #303133;
}

.login-form, .register-form {
  margin-top: 20px;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 0 0 20px;
}

.submit-btn {
  width: 100%;
  padding: 12px 0;
  font-size: 16px;
}

.form-footer {
  text-align: center;
  margin-top: 20px;
  color: #606266;
}

.form-footer p {
  margin: 0;
  font-size: 14px;
}

:deep(.el-input__wrapper) {
  padding: 4px 11px;
}

:deep(.el-input__inner) {
  height: 40px;
}

@media (max-width: 480px) {
  .login-card {
    margin: 0 20px;
  }
  
  .welcome-section h1 {
    font-size: 2em;
  }
}
</style> 